package com.g2.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.g2.admin.common.constant.StatusTypeEnums;
import com.g2.admin.model.po.AuthRolePo;
import com.g2.admin.model.po.AuthSystemPo;
import com.g2.admin.model.qo.RoleQo;
import com.g2.admin.service.AuthPermitRoleService;
import com.g2.admin.service.AuthRoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
@Api(tags = "Role")
public class RoleController extends BaseController {
    @Autowired
    private AuthRoleService authRoleService;
    @Autowired
    private AuthPermitRoleService authPermitRoleService;

    @GetMapping("/roles")
    public List<AuthRolePo> roles(RoleQo roleQo) {
        return authRoleService.getAuthRoles(roleQo);
    }

    @GetMapping("/list")
    public IPage<AuthRolePo> rolesAndPage(RoleQo roleQo) {
        return authRoleService.getAuthRoleAndPage(roleQo);
    }

    @GetMapping("/role/{id}")
    public AuthRolePo role(@PathVariable("id") long authId) {
        return authRoleService.getAuthrole(authId);
    }

    @PostMapping("/save")
    public void saveRole(@RequestBody AuthRolePo rolePo) {
        if (null == rolePo.getId()) {
            authRoleService.addAuthrole(rolePo);
        } else {
            authRoleService.updateAuthrole(rolePo);
        }
    }

    @PostMapping("/enable/{id}")
    public void enableRole(@PathVariable("id") Long id) {
        authRoleService.updateAuthrole(AuthRolePo.builder().id(id).status(StatusTypeEnums.StatusEnums.ENABLE).build());
    }

    @PostMapping("/disable/{id}")
    public void disableRole(@PathVariable("id") Long id) {
        authRoleService.updateAuthrole(AuthRolePo.builder().id(id).status(StatusTypeEnums.StatusEnums.DISABLE).build());
    }

    @PostMapping("/del/{id}")
    public void delRole(@PathVariable("id") long roleId) {
        authRoleService.delAuthrole(roleId);
    }

    @PostMapping("/del/{id}")
    public void delRole(@PathVariable("id") long roleId) {
        authPermitRoleService.delAuthrole(roleId);
    }
}
